//
//  PersonalDetailsViewController.m
//  SeaMelonSeeds_Counselor
//
//  Created by 尤振宇 on 2017/7/18.
//  Copyright © 2017年 杨笑. All rights reserved.
//

#import "PersonalDetailsViewController.h"
#import "PersonalDataViewController.h"
#import "TestTableViewController.h"
#import "MedicalRecordsViewController.h"
#import "UserDataApi.h"

@interface PersonalDetailsViewController ()
@property(nonatomic,strong)UISegmentedControl *control;
@property(nonatomic,strong)UIView *showView;
@property(nonatomic,strong)UserData *userData;

@end

@implementation PersonalDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"个人详情";
    self.view.backgroundColor = [UIColor whiteColor];
    [self initView];
}

+(PersonalDetailsViewController *)PersonalDetailsViewController:(Order *)order UserID:(int)userID
{
    PersonalDetailsViewController *vc = [[PersonalDetailsViewController alloc]init];
    //子页面用userData ID请求
    vc.userData = [[UserData alloc]init];
    vc.userData.id = userID;
    vc.order = order;
    return vc;
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    if (self.isRecords) {
//        self.control.selectedSegmentIndex = 2;
//        [self switchAction:self.control];
//    }
}

-(void)initView
{
    [self.view addSubview:self.control];
    [self.control mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).and.offset(38);
        make.width.equalTo(self.view).and.offset(-(38 * 2));
        make.top.equalTo(self.view).with.offset(13 + 64);
        make.height.mas_equalTo(25);
    }];
    
    [self.view addSubview:self.showView];
    [self.showView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.control.mas_bottom).and.offset(13);
        make.width.equalTo(self.view);
        make.leading.equalTo(self.view);
        make.height.mas_equalTo(SCREENH_HEIGHT - 26 - 64 - 25);
    }];
    
    PersonalDataViewController *personalVC = [[PersonalDataViewController alloc]init];
    personalVC.userData = self.userData;
    personalVC.order = self.order;
    [self.showView addSubview:personalVC.view];
    [self addChildViewController:personalVC];
    [personalVC.view mas_makeConstraints:^(MASConstraintMaker *make){
        make.edges.equalTo(self.showView);
    }];
    
    TestTableViewController *testVC = [[TestTableViewController alloc]init];
    testVC.userData = self.userData;
    testVC.order = self.order;
    [self.showView addSubview:testVC.view];
    [self addChildViewController:testVC];
    [testVC.view mas_makeConstraints:^(MASConstraintMaker *make){
        make.edges.equalTo(self.showView);

    }];
    
    MedicalRecordsViewController *medicalVC = [[MedicalRecordsViewController alloc]init];
    medicalVC.userData = self.userData;
    medicalVC.order = self.order;
    [self.showView addSubview:medicalVC.view];
    [self addChildViewController:medicalVC];
    [medicalVC.view mas_makeConstraints:^(MASConstraintMaker *make){
        make.edges.equalTo(self.showView);
    }];
    
    [self switchAction:self.control];
    
    [personalVC loadData];
    [testVC loadData];
    if (self.order.allow_check){
        [medicalVC loadData];
    }
}

- (void)switchAction:(UISegmentedControl *)control
{
    if ((control.selectedSegmentIndex == 1) || (control.selectedSegmentIndex == 2)) {
        if (!self.order.allow_check || !self.order) {
            UIAlertController *vc = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有权限查看" preferredStyle:(UIAlertControllerStyleAlert)];
            [vc addAction:[UIAlertAction actionWithTitle:@"确认" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
                [vc dismissViewControllerAnimated:YES completion:nil];
            }]];
            [self presentViewController:vc animated:YES completion:nil];
        }
    }
    [self.showView bringSubviewToFront:self.childViewControllers[control.selectedSegmentIndex].view];
}

-(UISegmentedControl *)control
{
    if (!_control) {
        _control = [[UISegmentedControl alloc]initWithItems:@[@"个人资料",@"测试量表",@"咨询履历"]];
        _control.tintColor = k65ACFE;
        _control.selectedSegmentIndex = 0;
        [_control addTarget:self action:@selector(switchAction:) forControlEvents:(UIControlEventValueChanged)];
    }
    return _control;
}

-(UIView *)showView
{
    if (!_showView) {
        _showView = [[UIView alloc]init];
        _showView.backgroundColor = kF2F2F2;
    }
    return _showView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
